<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :table-loading="loading"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :page="page"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="{row}" slot="open">
        <el-switch @change="rowSwitchChange(row)" v-model="row.open"></el-switch>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  remove,
  update,
  add,
  updateOpen
} from "@/api/admin/msgTemplate";
import { mapGetters } from "vuex";

export default {
  data () {
    return {
      form: {},
      query: {},
      loading: true,
      rolesTree: [],
      rolesKeys: [],
      box: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        menu:true,
        size: 'mini',
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        addBtn:true,
        editBtn:true,
        delBtn:false,
        excelBtn:false,
        rowKey: 'id',
        height: 'auto',
        // calcHeight: 380,
        dialogType: 'drawer',
        menuWidth: 120,
        searchMenuSpan:7,
        column: [
          {
            label: "模板标题",
            prop: "title",
            search:true,
            rules: [{
              required: true,
              message: "请输入模板标题",
              trigger: "blur"
            }]
          },
          {
            label: "模板说明",
            prop: "info",
            rules: [{
              required: true,
              message: "请输入模板说明",
              trigger: "blur"
            }]
          },
          {
            label: "标识",
            prop: "mark",
            rules: [{
              required: true,
              message: "请输入标识",
              trigger: "blur"
            }]
          },
          {
            label: "是否开启",
            prop: "open",
            slot:true,
            display:false,
            rules: [{
              required: true,
              message: "请输入是否开启 0否  1是",
              trigger: "blur"
            }]
          },
          {
            label: "模板类型",
            prop: "type",
            search:true,
			type:'select',
			dicData:[{
				label:'系统消息',
				value:'msg'
			}
			// ,{
			// 	label:'微信推送',
			// 	value:'wx'
			// }
			,{
				label:'短信',
				value:'sms'
			},
            {
              label:'app',
              value:'app'
            }],
            rules: [{
              required: true,
              message: "请输入模板类型 msg系统消息 wx微信推送 sms短信 app app推送通知",
              trigger: "blur"
            }]
          },
       /*   {
            label: "模板编号",
            prop: "templateCode",
            // search:true,
            rules: [{
              required: true,
              message: "请输入模板编号",
              trigger: "blur"
            }]
          },*/
          {
            label: "模板内容",
            prop: "content",
            type: "textarea",
            fixed: true,
			search:true,
            rules: [{
              required: true,
              message: "请输入模板内容",
              trigger: "blur"
            }]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        viewBtn: this.vaildData(this.permission["/mall-cloud-base/template/detail"], true),
        delBtn: this.vaildData(this.permission["/mall-cloud-base/template/delete"], true),
        editBtn: this.vaildData(this.permission["/mall-cloud-base/template/editItem"], true)
      };
    },
  },
  methods: {
    rowSave (row, loading, done) {
      row.position = row.positionIds;
      debugger
      row.deptName = row.$deptId;
      row.repassword = row.password;
      add(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        console.log(error);
      });
    },
    rowUpdate (row, index, loading, done) {
      row.position = row.positionIds;
      update(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();
        console.log(error);
      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params) {
		this.currentChange(1)
      this.query = params;
      this.onLoad(this.page, params);
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        this.data = data.data;
      });
    },
    rowSwitchChange(row) {
      if (row.open) {
        updateOpen(row.id, "true");
      } else {
        updateOpen(row.id, "false");
      }
    },
  }
};
</script>

<style>
</style>
